package com.backwaveweibo.bww.controller;

import com.backwaveweibo.bww.entity.User;
import com.backwaveweibo.bww.service.FollowService;
import com.backwaveweibo.bww.service.UserService;
import com.backwaveweibo.bww.utils.JwtTokenUtil;
import com.backwaveweibo.bww.utils.Result;
import com.backwaveweibo.bww.utils.ResultCode;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 关注功能接口
 */
@RestController
@Slf4j
@RequestMapping("follow")
public class FollowController {
    @Resource
    FollowService followService;
    @Resource
    UserService userService;


    /**
     * 一个用户是否是另一个用户的粉丝
     * @param fansId 想要辨别人的id
     * @return
     */
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    @GetMapping("isFans")
    public Result isFans(Long fansId) {

        boolean fans = followService.isFans(fansId, JwtTokenUtil.getUser());
        return new Result().setData(fans);

    }

    /**
     * 是否关注
     * @param followId 被关注人的id
     * @return
     */
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    @GetMapping("isSubscribe")
    public Result isSubscribe(Long followId){
        Boolean subscribe = followService.isSubscribe(followId, JwtTokenUtil.getUser());
        return new Result().setData(subscribe);
    }

    /**
     * 关注一个人
     * @param followId 被关注人的id
     * @return
     */
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    @PostMapping("follow")
    public Result followSomeone(@RequestBody Map followId) {
        Object sfollowId =  followId.get("followId");
        String s = sfollowId.toString();
        log.info("followID {}",followId);
        Long aLong = Long.valueOf(s);
        Boolean follow = followService.setFollow(aLong, JwtTokenUtil.getUser());
        return new Result().setData(follow).setCode(follow?ResultCode.SUCCESS:ResultCode.FAIL).setMessage(follow?"关注成功":"关注失败");
    }

    /**
     * 取关某人
     * @param unFollowId 想要取关人的id
     * @return
     */
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    @PostMapping("unfollow")
    public Result unFollowSomeone(Long unFollowId) {
        Boolean unFollow = followService.setUnFollow(unFollowId, JwtTokenUtil.getUser());
        return new Result().setData(unFollow);
    }

    /**
     * 获取粉丝列表
     * @param id
     * @param pageNumber
     * @param pageSize
     * @return
     */

    @GetMapping("getFans")
    public Result getFans(Long id, Integer pageNumber, Integer pageSize) {
        List<Long> myFans =null;
        if(id!=null){
            myFans = followService.getMyFans(userService.findUser(new User().setUserId(id)));
        }

        //pageNumber=pageNumber == null?1:pageNumber;
        //pageSize = pageSize == null?1:pageSize;
        if (myFans.size()>0) {
            //List<Long> longs = myFans.subList((pageNumber - 1) * pageSize, (pageNumber - 1) * pageSize + pageSize);
            log.info("找到的粉丝列表{}",myFans);
            List<User> users = userService.batchSelectUser(myFans);

            return new Result().setData(users);
        } else {
            return new Result().setMessage("你没有粉丝").setCode(ResultCode.SUCCESS);
        }
    }

    /**
     * 获取关注数
     * @param id
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("getSubscribe")
    public Result getSubscribe(Long id,Integer pageNumber, Integer pageSize) {
        List<Long> mySubscribe = null;
        if (id!=null){
             mySubscribe = followService.getMySubscribe(id);
        }
        pageNumber=pageNumber == null?1:pageNumber;
        pageSize = pageSize==null?1:pageSize;
        if (mySubscribe.size()>0) {
            //List<Long> longs = mySubscribe.subList((pageNumber - 1) * pageSize, (pageNumber - 1) * pageSize + pageSize);
            //log.info("找到的用户id列表==>{}",longs);
            List<User> users = userService.batchSelectUser(mySubscribe);
            return new Result().setData(users).setCode(ResultCode.SUCCESS);
        }else {
            return new Result().setMessage("你没有关注").setCode(ResultCode.SUCCESS);
        }
    }

    /**
     * 获取粉丝数
     * @param id
     * @return
     */
    @GetMapping("getFansCount")
    public Result getFansCount(Long id) {
        Long fansCont = followService.followMeCount(new User().setUserId(id));
        log.info("获取粉丝数为{}",fansCont);
        return new Result().setData(fansCont).setMessage("粉丝数量为").setCode(ResultCode.SUCCESS);
    }

    /**
     * 获取订阅数
     * @param id
     * @return
     */

    @GetMapping("getSubscribeCount")
    public Result getSubscribeCount(Long id) {
        Long mySubscribeCount = followService.myFollowCount(new User().setUserId(id));
        log.info("获取到订关注数量 {}",mySubscribeCount);
        return new Result().setData(mySubscribeCount).setMessage("关注数量为").setCode(ResultCode.SUCCESS);
    }
}
